home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / ch2 / Stopwatch2 / Timer.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.3 KB  |  98 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. //////////////////////////////////////////////////
  22. // Timer.C: A clock class for the stopwatch
  23. ///////////////////////////////////////////////////
  24. #include "Timer.h"
  25. #include "Face.h"
  26. #include <Xm/Xm.h>
  27.  
  28. Timer::Timer ( XtAppContext app, Face *face, int interval )
  29. {
  30.     _face     = face;
  31.     _id       = NULL;
  32.     _app      = app;
  33.     _counter  = 0;
  34.     _interval = interval;
  35. }
  36.  
  37. void Timer::start()
  38. {
  39.     // Reset the elapsed time
  40.     
  41.     _counter = 0;
  42.     
  43.     if ( _id ) // If a previous callback is still in effect, remove it
  44.     {
  45.     XtRemoveTimeOut ( _id );
  46.     _id = NULL;
  47.     }
  48.     
  49.     // Register a function to be called in _interval milliseconds
  50.     
  51.     _id = XtAppAddTimeOut ( _app, 
  52.                _interval, 
  53.                &Timer::tickCallback, 
  54.                (XtPointer) this );
  55. }
  56.  
  57. void Timer::stop()
  58. {
  59.     // Remove the current timeout function, if any
  60.     
  61.     if ( _id )
  62.     XtRemoveTimeOut ( _id );
  63.     
  64.     _id = NULL;
  65. }
  66.  
  67. void Timer::tickCallback ( XtPointer clientData, XtIntervalId * )
  68. {
  69.     // Get the object pointer and call the corresponding tick function
  70.     
  71.     Timer *obj = (Timer *) clientData;
  72.     
  73.     obj->tick();
  74. }
  75.  
  76. void Timer::tick()
  77. {
  78.     // Increment a counter for each tick
  79.     
  80.     _counter++;  
  81.     
  82.     // Display the current time in the Face object
  83.     
  84.     _face->setTime (  elapsedTime() );
  85.     
  86.     // Reinstall the timeout callback
  87.     
  88.     _id = XtAppAddTimeOut ( _app, 
  89.                _interval, 
  90.                &Timer::tickCallback, 
  91.                (XtPointer) this );
  92. }
  93.  
  94. float Timer::elapsedTime()
  95. {
  96.     return ( (float) _counter * _interval / 1000.0 );
  97. }
  98.